package com.xy.mvvmdemo.fragment

import android.nfc.Tag
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

/**
 * @ClassName LazyFragment
 * @Author xy
 * @Date 2021/7/8 11:35
 * @Description Fragment 懒加载
 */
@Suppress("DEPRECATION")
abstract class LazyFragment:Fragment() {

    private val TAG = "生命周期"
    var rootView:View?=null
    var isViewCreated = false
    var currentVisibleState = false //当前页面是否可见
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        Log.i(TAG, "onCreateView: ")
        if (rootView==null){
            rootView = inflater.inflate(setLayoutRes(),container,false)
        }
//        rootView?.let { initView(it) }

        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.i(TAG, "onViewCreated: ")
        isViewCreated = true
        if (userVisibleHint){
            startFragment()
        }
        initData(view,savedInstanceState)
    }

    abstract fun initData(view: View,savedInstanceState: Bundle?)


    protected abstract fun setLayoutRes():Int

    //fragment 是否可见
    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        Log.i(TAG, "setUserVisibleHint: ")
        if (isViewCreated){ //界面是否创建
            if (isVisibleToUser&&!currentVisibleState){ //当前界面不可见初始化数据
                if (currentVisibleState == isVisibleToUser) return
                startFragment()
            }else if (!isVisibleToUser&&currentVisibleState){
                if (currentVisibleState == isVisibleToUser) return
                stopFragment()
            }
            currentVisibleState = isVisibleToUser
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Log.i(TAG, "onDestroyView: ")
         isViewCreated = false
         currentVisibleState = false //当前页面是否可见
        Log.i(TAG, "name:${this.javaClass.simpleName} --->onDestroyView")
    }

    override fun onResume() {
        super.onResume()
        Log.i(TAG, "onResume: ")
        if (userVisibleHint&&!currentVisibleState){
            startFragment()
        }
        Log.i(TAG, "name:${this.javaClass.simpleName} --->onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.i(TAG, "onPause: ")
        if (userVisibleHint&&currentVisibleState){
            stopFragment()
        }
        Log.i(TAG, "name:${this.javaClass.simpleName} --->onPause")
    }

    //开始Fragment
    abstract fun startFragment()

    //暂停fragment
    abstract fun stopFragment()

    override fun onStart() {
        super.onStart()
        Log.i(TAG, "onStart: ")
    }

    override fun onStop() {
        super.onStop()
        Log.i(TAG, "onStop: ")
    }

    override fun onDestroy() {
        super.onDestroy()
        currentVisibleState = false
        Log.i(TAG, "onDestroy: ")
    }


}